package org.tools.wstt.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

/**
 * Servlet implementation class AboutServlet
 */
public class DispatcherServlet extends AHttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest pRequest, HttpServletResponse pResponse)
			throws ServletException, IOException {
		log("req uri : " + pRequest.getRequestURI());
		StringBuffer vTarget = new StringBuffer(INTERNAL_PATH_OF_JSPS);
		String vPathInfo = pRequest.getPathInfo();
		if (StringUtils.isEmpty(vPathInfo) || vPathInfo.indexOf("..") != -1) {
			log("Bad PathInfo [" + vPathInfo + "]: just go to about");
			vTarget.append("/about");
		}
		else {
			vTarget.append(vPathInfo);
		}
		vTarget.append(".jsp");
		
		RequestDispatcher vReqDispatcher = pRequest.getRequestDispatcher(vTarget
				.toString());
		vReqDispatcher.forward(pRequest, pResponse);
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest pRequest,
			HttpServletResponse pResponse) throws ServletException, IOException {
		this.doGet(pRequest, pResponse);
	}

}
